﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using Samurai.DAL;
using Samurai.Infrastructure;

namespace Samurai.Client.Commands
{
    public class NotAMovieCommand : CommandBase
    {
        private readonly ObservableCollection<Movie> _movies;
        private readonly ObservableCollection<GenreItem> _genres;

        public NotAMovieCommand(ObservableCollection<Movie> movies, ObservableCollection<GenreItem> genres)
        {
            _movies = movies;
            _genres = genres;
        }

        protected override void InvokeExecute(object parameter)
        {
            if (parameter != null)
            {
                var confirm = MessageBox.Show("Are you sure?", "Confirm Delete", MessageBoxButton.YesNo);

                if (confirm == MessageBoxResult.Yes)
                {
                    if (LongOperationStarted != null)
                        LongOperationStarted();


                    var movieData = (Movie)parameter;
                    Manager.Instance.MarkNotAMovie(movieData);

                    _movies.Clear();
                    _genres.Clear();

                    Movie[] movies = null;
                    GenreItem[] genres = null;

                    Application.Current.Dispatcher.Invoke(
                        new Action(
                            () =>
                            {
                                movies = Manager.Instance.GetNewValidMovies(_movies);
                                genres = Manager.Instance.GetNewGenres(_genres);
                            }
                            )
                        );

                    foreach (var movie in movies)
                    {
                        _movies.Add(movie);
                        System.Windows.Forms.Application.DoEvents();
                    }

                    foreach (var genre in genres)
                    {
                        _genres.Add(genre);
                        System.Windows.Forms.Application.DoEvents();
                    }

                    if (LongOperationFinished != null)
                        LongOperationFinished();

                }
            }
        }
    }
}
